﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CCWin;
using GroupChat2013.Entity;
using GroupChat2013.Service;
using GroupChat2013.Common;
using GroupChat2013.Logic;

namespace GroupChat2013.Main
{
    public partial class FrmGroupManager : CCSkinMain
    {
        private List<GroupInfo> groupList = new List<GroupInfo>();

        public FrmGroupManager()
        {
            InitializeComponent();
            gvGroups.AutoGenerateColumns = false;

            this.Load += new EventHandler(FrmGroupManager_Load);
        }

        void FrmGroupManager_Load(object sender, EventArgs e)
        {
            InitGroupData();
        }

        private void InitGroupData()
        {
            gvGroups.Rows.Clear();
            try
            {
                AsyncClient.AsyncCall<List<GroupInfo>>(() =>
                {
                    return ServiceUrlProxy.GetInstance().GetManageGroupList(
                        EngineContext.Current.CurrUserInfo.CID);
                },
                (groupList) =>
                {
                    if (groupList != null && groupList.Count > 0)
                    {
                        //gvGroups.DataSource = groupList;
                        groupList.ForEach(x =>
                        {

                            DataGridViewRow dr = new DataGridViewRow();
                            foreach (DataGridViewColumn c in gvGroups.Columns)
                            {
                                dr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);
                            }

                            dr.Cells[0].Value = x.groupId;
                            dr.Cells[1].Value = x.groupName;
                            dr.Cells[2].Value = x.createrName;

                            gvGroups.Rows.Add(dr);
                        });

                        gvGroups_CellClick(gvGroups, null);
                    }
                });
            }
            catch (Exception ex)
            {
                LogHelper.LogError(string.Format("GetManageGroupList()方法 错误信息：{0} 堆栈信息：{1} ", ex.Message, ex.StackTrace));
            }
        }

        private void tsbCreateGroup_Click(object sender, EventArgs e)
        {
            FrmAddGroup addgroup = new FrmAddGroup(null);
            
            addgroup.CompletedHandler += () =>
            {
                InitGroupData();
            };

            addgroup.ShowDialog();
        }

        private void tsbModifyGroup_Click(object sender, EventArgs e)
        {
            if (gvGroups.SelectedRows != null && gvGroups.SelectedRows.Count > 0)
            {
                DataGridViewRow row = gvGroups.SelectedRows[0];

                GroupInfo groupInfo = new GroupInfo()
                {
                    groupId = row.Cells[0].Value.ToString(),
                    groupName = row.Cells[1].Value.ToString()
                };

                FrmAddGroup addgroup = new FrmAddGroup(groupInfo);
                
                addgroup.CompletedHandler += () =>
                {
                    InitGroupData();
                };

                addgroup.ShowDialog();
            }
        }

        private void tsbDeleteGroup_Click(object sender, EventArgs e)
        {
            if (gvGroups.SelectedRows != null && gvGroups.SelectedRows.Count > 0)
            {
                DataGridViewRow row = gvGroups.SelectedRows[0];
                string groupId = row.Cells[0].Value.ToString();
                string groupName = row.Cells[1].Value.ToString();

                if (MessageBoxEx.Show("是否删除群 " + groupName, "提示",
                         MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
                {
                    AsyncClient.AsyncCall<bool>(() =>
                       {
                         return  ServiceUrlProxy.GetInstance().DeleteGroup(groupId,
                                             EngineContext.Current.CurrUserInfo.CID);
                       },
                       (succ) =>
                       {
                           if (succ)
                           {
                               MessageBoxEx.Show("删除成功 " + groupName, "提示",
                                         MessageBoxButtons.OK, MessageBoxIcon.Information);

                               InitGroupData();
                               return;
                           }
                           else
                               MessageBoxEx.Show("删除失败 " + groupName, "提示",
                                         MessageBoxButtons.OK, MessageBoxIcon.Information);
                       });
                }
            }
        }

        private void tsbGroupUserManage_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = gvGroups.SelectedRows[0];

            GroupInfo groupInfo = new GroupInfo()
            {
                groupId = row.Cells[0].Value.ToString(),
                groupName = row.Cells[1].Value.ToString()
            };

            FrmGroupUserManager addgroup = new FrmGroupUserManager(groupInfo);
            addgroup.ShowDialog();
        }

        private void tsbUpdateGroupNote_Click(object sender, EventArgs e)
        {
            if (gvGroups.SelectedRows != null && gvGroups.SelectedRows.Count > 0)
            {
                DataGridViewRow row = gvGroups.SelectedRows[0];

                GroupInfo groupInfo = new GroupInfo()
                {
                    groupId = row.Cells[0].Value.ToString(),
                    groupName = row.Cells[1].Value.ToString(),
                    groupDesc = row.Tag.ToString()
                };

                FrmGroupNotice groupNotice = new FrmGroupNotice(groupInfo);

                groupNotice.CompletedHandler += (notice) =>
                {
                    gvGroups.SelectedRows[0].Tag = notice;
                    InitGroupData();
                };

                groupNotice.ShowDialog();
            }
        }

        private void gvGroups_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvGroups.SelectedRows != null && gvGroups.SelectedRows.Count > 0)
            {
                DataGridViewRow row = gvGroups.SelectedRows[0];
                SetNoticeContent(row);
            }
        }

        private void SetNoticeContent(DataGridViewRow row)
        {
            string groupId = row.Cells[0].Value.ToString();
            lbGroupName.Text = row.Cells[1].Value.ToString();

            if (row.Tag != null)
            {
                tbNotice.SkinTxt.Text = row.Tag.ToString();
                return;
            }
            else
            {
                try
                {
                    AsyncClient.AsyncCall<List<Notice>>(() =>
                    {
                        return ServiceUrlProxy.GetInstance().GetGroupNotice(groupId, EngineContext.Current.CurrUserInfo.CID, 0, 1);
                    },
                    (notices) =>
                    {
                        if (notices != null)
                        {
                            tbNotice.SkinTxt.Text = notices.Count == 0 || string.IsNullOrEmpty(notices[0].groupContent) ?
                                "" : notices[0].groupContent;
                        }
                        else
                        {
                            tbNotice.SkinTxt.Text = "";
                        }
                        row.Tag = tbNotice.SkinTxt.Text;
                    });
                }
                catch { row.Tag = tbNotice.SkinTxt.Text = ""; }
            }
        }
    }
}
